from selenium.webdriver.common.by import By as by
from Page.UserPage.ErpLoginPage import ErpLoginPage
import random
import time


class CreateNewProduct(ErpLoginPage):

    """业务层"""

    # 商品名字
    goods_name = '红米K'
    for i in range(5):
        goods_name = goods_name + str(random.randint(0, 9))
    # 零售管理
    retail_management = (by.XPATH, '(//div[@class="ant-menu-submenu-title"])[1]')

    # 零售出库
    retail_delivery = (by.XPATH, '//a[@target="零售出库"]')

    # 新增按钮
    new_order = (by.CSS_SELECTOR, '.table-operator .ant-btn-primary')

    # 点击引导关闭
    skip_button = (by.CSS_SELECTOR, '.introjs-skipbutton')

    # 会员卡号点击
    click_vip = (by.CSS_SELECTOR, '#organId')

    # 会员卡号输入框
    input_vip = (by.CSS_SELECTOR, '#organId .ant-select-search__field')

    # 选择商品
    select_goods = (by.XPATH, '//span[@class="ant-input-group ant-input-group-compact"]/button')

    # 点击新增商品按钮,这里还有一个引导要关闭
    new_product_button = (by.CSS_SELECTOR,'div.ant-row>span.table-page-search-submitButtons:nth-of-type(1)>*>button:nth-of-type(3)')

    # 输入名称,名称不能重复
    input_name = (by.XPATH, '//input[@placeholder="请输入名称"]')

    # 输入单位
    input_union = (by.XPATH, '//input[@placeholder="输入单位"]')

    # 输入采购价
    input_purchase_price = (by.XPATH, '//input[@placeholder="请输入采购价"]')

    # 输入零售价
    input_retail_price = (by.XPATH,'//input[@placeholder="请输入零售价"]')

    # 输入销售价
    input_sale_price = (by.XPATH, '//input[@placeholder="请输入销售价"]')

    # 输入最低售价
    input_minimum_price = (by.XPATH, '//input[@placeholder="请输入最低售价"]')

    # 保存按钮
    save = (by.CSS_SELECTOR, 'div.ant-modal-footer>button.ant-btn-primary:nth-of-type(2)')

    # 选择刚刚新建商品
    new_product = (by.CSS_SELECTOR,'tbody.ant-table-tbody:nth-child(3)>tr.ant-table-row:nth-of-type(1)')
    """这个可能有问题"""

    # 查看刚刚的库存，只获取了文本
    data_num = (by.CSS_SELECTOR, 'tbody.ant-table-tbody:nth-child(3)>tr:nth-of-type(1)>td:nth-of-type(10)')

    # 确定按钮
    determine = (by.XPATH, '(//button[@class="ant-btn ant-btn-primary"])[8]')

    # 保存并审核
    save_check = (by.CSS_SELECTOR, 'div.ant-modal-footer>button:nth-of-type(3)')

    # 关闭按钮
    close_window = (by.XPATH, '(//button[@class="ant-modal-close"])[2]')


    """操作层"""
    def open_page(self):
        """打开零售出库页面"""
        self.click(self.retail_management)
        time.sleep(1)
        self.click(self.retail_delivery)
        """点击新增并输入会员号"""
        self.click(self.new_order)
        self.click(self.skip_button)
        time.sleep(1)
        self.click(self.click_vip)
        self.send_keys(self.input_vip,"12314")

    def add_product(self):
        """打开新增商品页面"""
        self.click(self.select_goods)
        time.sleep(1)
        self.click(self.new_product_button)
        self.click(self.skip_button)
        """输入商品信息"""
        self.send_keys(self.input_name,self.goods_name)
        self.send_keys(self.input_union,"部")
        time.sleep(1)
        self.send_keys(self.input_purchase_price,"100")
        time.sleep(1)
        self.send_keys(self.input_retail_price,"100")
        time.sleep(1)
        self.send_keys(self.input_sale_price,"100")
        time.sleep(1)
        self.send_keys(self.input_minimum_price,"100")
        time.sleep(1)
        self.click(self.save)
        time.sleep(2)


    def get_inventory(self):
        """获取库存量"""
        text = self.get_element_text(self.data_num)
        num = int(text)
        return num

    def delivery_goods(self):
        time.sleep(2)
        self.js_click(self. new_product)  # 为啥这里要用js点击
        time.sleep(1)
        self.click(self.determine)
        self.click(self.save_check)
        time.sleep(2)







    """业务层"""

if __name__ == '__main__':
    cnp = CreateNewProduct()

    cnp.common_login()
    cnp.open_page()
    cnp.add_product()
    num1 = cnp.get_inventory()
    cnp.delivery_goods()

    cnp.click(cnp.new_order)
    cnp.click(cnp.select_goods)
    time.sleep(2)
    num2 = cnp.get_inventory()

    print(num1,num2)

    assert num1 - num2 == 1

